package cn.livingCloud.entity.system;

import cn.livingCloud.context.Config;
import cn.livingCloud.entity.user.User;
import cn.livingCloud.entity.user.UserAccount;
import cn.livingCloud.utils.DateUtils;
import cn.livingCloud.utils.SessionUtils;
import cn.livingCloud.utils.UuidGenerator;
import org.apache.commons.io.FilenameUtils;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.Date;

/**
 * 图片资源类
 * Created by 唐旭 on 2016/1/27.
 */
public class ImageResource implements Serializable {
    public static final String DEFAULT_PARENT = ".";
    public static final String IMAGE_URL_PREFIX = Config.getDomainUrl() + "image/";

    private String uuid = UuidGenerator.generate();
    //图片地址
    private String path;
    private String originalFileName;
    //创建时间
    private Date createTime = DateUtils.now();
    //扩展名
    private String extName;
    //创建人
    private User creater;
    private String createrId;

    private boolean archive = false;

    public ImageResource() {
    }

    public ImageResource(MultipartFile multipartFile) {
        Assert.notNull(multipartFile);
        this.originalFileName = multipartFile.getOriginalFilename();
        this.path = UuidGenerator.generate();
        this.extName = FilenameUtils.getExtension(originalFileName);

        File dir = checkPathExist(Config.UPLOAD_IMAGE_PATH);
        File file = new File(dir, path);
        try {
            multipartFile.transferTo(file);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        UserAccount account = (UserAccount) SessionUtils.getCurrentUserAccount();

        if (account != null) {
            this.creater = account.getUser();
            this.createrId = account.getUserId();
        }
    }

    public String getUrl() {
        return IMAGE_URL_PREFIX + uuid;
    }

    private File checkPathExist(String path) {
        File dir = new File(path);
        if (!dir.exists()) {
            boolean mkdirs = dir.mkdirs();
            if (!mkdirs) {
                throw new RuntimeException("create file error,check file path.");
            }
        }
        return dir;
    }


    public boolean isArchive() {
        return archive;
    }

    public ImageResource setArchive(boolean archive) {
        this.archive = archive;
        return this;
    }

    public String getOriginalFileName() {
        return originalFileName;
    }

    public ImageResource setOriginalFileName(String originalFileName) {
        this.originalFileName = originalFileName;
        return this;
    }

    public String getUuid() {
        return uuid;
    }

    public ImageResource setUuid(String uuid) {
        this.uuid = uuid;
        return this;
    }

    public String getPath() {
        return path;
    }

    public ImageResource setPath(String path) {
        this.path = path;
        return this;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public ImageResource setCreateTime(Date createTime) {
        this.createTime = createTime;
        return this;
    }

    public String getExtName() {
        return extName;
    }

    public ImageResource setExtName(String extName) {
        this.extName = extName;
        return this;
    }

    public User getCreater() {
        return creater;
    }

    public ImageResource setCreater(User creater) {
        this.creater = creater;
        return this;
    }

    public String getCreaterId() {
        return createrId;
    }

    public ImageResource setCreaterId(String createrId) {
        this.createrId = createrId;
        return this;
    }

    /**
     * UPLOAD_IMAGE_PATH=D:/images/
     * path=sdfsdfsdfdsfds
     * extName=jpg
     *
     * @return 文件
     */
    public File getFile() {
        return new File(Config.UPLOAD_IMAGE_PATH + path);
    }

    public byte[] getFileByte() {
        File file = getFile();
        FileInputStream inputStream = null;
        byte[] data;
        try {
            inputStream = new FileInputStream(file);
            data = new byte[(int) file.length()];
            int read = inputStream.read(data);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return data;
    }
}
